﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace EducationManagementSystem.Admin
{
    public partial class SchoolMange : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }

        private void Bind()
        {
            SqlConnection conn = DB.Conn();
            conn.Open();
            string sql = "select * from school  ";
            SqlCommand cmd = new SqlCommand(sql, conn);
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            sda.Fill(ds);
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text == "")
            {
                this.Terr.InnerHtml = "<font color='red'>学院名称不能为空</font>";

            }
            else
            {
                SqlConnection conn = DB.Conn();
                conn.Open();
                string sql = "select * from school where schoolname='" + TextBox1.Text + "' ";
                SqlCommand cmd = new SqlCommand(sql, conn);
                object a = cmd.ExecuteScalar();
                if (a != null)
                {
                    this.Page.RegisterStartupScript(" ", "<script>alert('有重复，请重新添加'); </script> ");
                    TextBox1.Text = "";
                }
                else
                {
                    string sql2 = "insert into school(schoolname) values('" + TextBox1.Text + "') ";
                    SqlCommand cmd2 = new SqlCommand(sql2, conn);
                    int a2 = cmd2.ExecuteNonQuery();
                    if (a2 > 0)
                    {
                        this.Page.RegisterStartupScript(" ", "<script>alert(' 添加成功 '); </script> ");

                        Bind();
                        this.Terr.InnerHtml = "";
                    }
                    else
                    {
                        this.Page.RegisterStartupScript(" ", "<script>alert(' 对不起，添加失败 '); </script> ");
                    }
                }
            }
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            Bind();
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {

            if (e.CommandName == "de")
            {
                string sid = e.CommandArgument.ToString();
                SqlConnection conn = DB.Conn();
                conn.Open();
                string sql = "delete from school where schoolid='" + sid + "'  ";
                SqlCommand cmd = new SqlCommand(sql, conn);
                int a = cmd.ExecuteNonQuery();
                if (a > 0)
                {
                    this.Page.RegisterStartupScript(" ", "<script>alert(' 删除成功 '); </script> ");
                    Bind();
                    this.Terr.InnerHtml = "";
                }
                else
                {
                    this.Page.RegisterStartupScript(" ", "<script>alert(' 对不起，删除失败 '); </script> ");
                }
            }
        }
    }
}